package com.yuandian.management.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.management.entity.ConfComponentIp;
import com.yuandian.api.management.entity.ConfIpDriftOffline;
import com.yuandian.api.management.entity.ConfSegment;
import com.yuandian.api.management.entity.ConfSystem;
import com.yuandian.management.mapper.ConfIpDriftOfflineMapper;
import com.yuandian.management.service.ConfComponentIpService;
import com.yuandian.management.service.ConfIpDriftOfflineService;
import com.yuandian.management.service.ConfSegmentService;
import com.yuandian.management.service.ConfSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 漂移IP下线表-Service实现
 * @author zfsn
 * @date 2023-3-15
 */
@Service
public class ConfIpDriftOfflineServiceImpl extends ServiceImpl<ConfIpDriftOfflineMapper, ConfIpDriftOffline>
		implements ConfIpDriftOfflineService {


	@Autowired
	private ConfComponentIpService confComponentIpService;

	@Autowired
	private ConfSegmentService confSegmentService;

	@Autowired
	private ConfIpDriftOfflineMapper confIpDriftOfflineMapper;

	@Autowired
	private ConfSystemService confSystemService;

	/**
	 * 分页查询
	 *
	 * @param confIpDriftOfflineParams 筛选条件
	 * @return 通用返回对象
	 */
	@Override
	public Page<ConfIpDriftOffline> listPage(ConfIpDriftOffline confIpDriftOfflineParams) {
		Page<ConfIpDriftOffline> page = confIpDriftOfflineMapper.listPage(new Page<>(confIpDriftOfflineParams.getPageNum(), confIpDriftOfflineParams.getPageSize()), confIpDriftOfflineParams);

		// 填充系统名称,组件名称和网端名称
		Map<Integer, String> confSystemMap = confSystemService.list().stream().collect(Collectors.toMap(ConfSystem::getId, ConfSystem::getName));
		List<ConfComponentIp> confComponents = confComponentIpService.list();
		Map<Integer, String> componentIps = confComponents.stream().collect(Collectors.toMap(ConfComponentIp::getId, ConfComponentIp::getIp));
		Map<Integer, String> componentPorts = confComponents.stream().collect(Collectors.toMap(ConfComponentIp::getId, ConfComponentIp::getPort));

		List<ConfSegment> confSegments = confSegmentService.list();
		Map<Integer, String> confSegmentNameMap = confSegments.stream().collect(Collectors.toMap(ConfSegment::getId, ConfSegment::getName));
		for (ConfIpDriftOffline confIpDriftOffline : page.getRecords()) {
			confIpDriftOffline.setSystemName(confSystemMap.get(confIpDriftOffline.getSystemId()));
			confIpDriftOffline.setComponentIp(componentIps.get(confIpDriftOffline.getIpComponentId()));
			confIpDriftOffline.setPort(componentPorts.get(confIpDriftOffline.getIpComponentId()));
			confIpDriftOffline.setSegmentName(confSegmentNameMap.get(confIpDriftOffline.getSegmentId()));
		}
		return page;
	}
}